//
//  PKAccountSafetyController.swift
//  Panku
//
//  Created by mac on 2017/2/14.
//  Copyright © 2017年 Monster. All rights reserved.
//

import UIKit
import SnapKit
import YYKit

private let PKAccountSafetyControllerCellID = "PKAccountSafetyControllerCellID"

class PKAccountSafetyController: PKBaseViewController {

    init(viewModel:PKAccountSafetyViewModel){
        self.viewModel = viewModel
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - properties
    //viewModel
    var viewModel:PKAccountSafetyViewModel?
    
    //UI
    lazy var topContainerView = PKBaseView()
    lazy var safetyImageView = UIImageView()
    lazy var safetyRankLabel = UILabel()
    
    lazy var tableView = PKBaseTableView { (make) in
        
    }
    //MARK: - life circle
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    //MARK: - UI
    override func setupRootView() {
        view.backgroundColor = UIColor.lightGray
    }
    
    override func setupSubViews() {
        view.addSubview(topContainerView)
        view.addSubview(safetyImageView)
        view.addSubview(safetyRankLabel)
        view.addSubview(tableView)
        
        topContainerView.backgroundColor = UIColor.white
        
        safetyRankLabel.text = "安全等级低"
        safetyRankLabel.font = UIFont.systemFont(ofSize: 15)
        safetyRankLabel.textColor = UIColor(hexString: "BF0B2B")
        
        tableView.dataSource = self
        tableView.delegate = self
        
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: PKAccountSafetyControllerCellID)
        
    }
    
    override func setupSubViewsConstraints() {
        topContainerView.snp.makeConstraints { (make) in
            make.left.equalTo(view.snp.left)
            make.right.equalTo(view.snp.right)
            make.top.equalTo(view.snp.top)
            make.height.equalTo(APP_WIDTH)
        }
        
        safetyImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(topContainerView.snp.centerX)
            make.centerY.equalTo(topContainerView.snp.centerY)
        }
        
        safetyRankLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(view.snp.centerX)
            make.top.equalTo(safetyImageView.snp.bottom).offset(20)
        }
        
        tableView.snp.makeConstraints { (make) in
            make.left.equalTo(view.snp.left)
            make.right.equalTo(view.snp.right)
            make.bottom.equalTo(view.snp.bottom)
            make.top.equalTo(topContainerView.snp.bottom).offset(35)
            
        }
    }
    
    override func setupNavigationBar() {
        self.title = "账号与安全"
    }
    
    override func bindViewModel() {
        
    }


}

extension PKAccountSafetyController:UITableViewDataSource,UITableViewDelegate{
    func numberOfSections(in tableView: UITableView) -> Int {
        return viewModel!.numberOfSections()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel!.numberOfRowsIn(Section: section)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: PKAccountSafetyControllerCellID, for: indexPath)
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        let title = viewModel!.titleForRowAt(Indexpath: indexPath)
        cell.textLabel?.text = title
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}
